module.exports = function($scope,$resource,$state,$modalInstance,$window){
	 $scope.currentid ;
     $scope.currentcode ;
     $scope.currentname ;
	$resource('/a/sys/office/treeData', {}, {}).query({}, function(res){
		  $scope.currentid = res[0].id;
          $scope.currentcode = res[0].code;
          $scope.currentname = res[0].name;
		  $scope.code = $scope.currentcode;
		  $scope.obj['company.id'] = $scope.currentid;
          $scope.obj['office.id'] = $scope.currentid;
    });    
     $scope.company = {};
     $scope.obj = {};
     $scope.obj.loginName = '';
     $scope.obj.name = '';
     var idobj = {};
    // $scope.reloadRoute = function () {
	// 	$window.location.reload();
	// };
	$scope.cancel = function () {
                $modalInstance.dismiss('cancel');              
				//$scope.reloadRoute();
    };
    $resource('/a/sys/user/ajaxform', {}, {}).get({}, function(res){
        $scope.objs = res.allRoles;
        $scope.company = res.company;
    });
    $scope.ok = function(){
        if($scope.obj.loginName === '')
        {
            alert('用户名必填');
            return;
        }

        if($scope.obj.name === '')
        {
            alert('姓名必填');
            return;
        }

        $scope.obj.roleIdList = [];
        angular.forEach(idobj, function (value, key) {
            $scope.obj.roleIdList.push(key)
        });

        if($scope.obj.roleIdList.length === 0)
        {
            alert('请选择角色');
            return;
        }

        $scope.obj.no = $scope.obj.loginName;
        $scope.obj.loginName = $scope.obj.loginName;
        $scope.obj.newPassword = '000000';
        $scope.obj.confirmNewPassword = '000000';
        $scope.obj.loginFlag = '1';
        var savedata={};
		savedata.name=$scope.obj.name;
		savedata.mobile=$scope.obj.phone;
        savedata.remark=$scope.obj.remarks;
        savedata.loginname=$scope.code+$scope.obj.loginName;
        $resource('/a/sys/user/ajaxsave', {}, {}).save($scope.obj, {}, function(res){
           if(res.errcode==0){                  
                	$resource('/api/ac/ggc/equipmentPerManageService/create', {}, {}).save(savedata, function(res){
                		if(res.errcode==0){
                			if(confirm("是否继续添加")){
                                $scope.obj.name="";
                                $scope.obj.phone="";
                                $scope.obj.remarks="";
                                $scope.obj.loginName="";                     
                			}else{
                				  $modalInstance.dismiss('cancel');
                				  //$scope.reloadRoute();	
                			}				
                		}else{
                			alert(res.errmsg);
                		}
                	});
           }else{
                	alert(res.errmsg);
                }

    //         $modalInstance.close(officeid, officename);

        });

    };


    
    $scope.selection = function($event, obj){


        var checkbox = $event.target;
        
        if(checkbox.checked)
        {
            idobj[obj.id] = '';
        }
        else
        {
           delete idobj[obj.id];
        }

    };
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//  $scope.equipmentperson={};
	// //用户名非空验证
	// $scope.equipmentperson.nameadd=function(){
    // $scope.equipmentperson.namecheck=false;
	// }
	// $scope.equipmentperson.nameblur=function(){
	// 	if ($scope.equipmentperson.name==undefined || $scope.equipmentperson.name==""){
	// 				$scope.equipmentperson.namecheck=true;
	// 			}
	// }
	// //维护周期非空验证
	// $scope.equipmentperson.mobileadd=function(){	
    // $scope.equipmentperson.mobilecheck=false;
	// }
	// $scope.equipmentperson.mobileblur=function(){
	// 	if ($scope.equipmentperson.mobile==undefined || $scope.equipmentperson.mobile==""){
	// 				$scope.equipmentperson.mobilecheck=true;
	// 			}
	// }
	// $scope.reloadRoute = function () {
	// 	$window.location.reload();
	// };
	// $scope.cancel = function () {
    //             $modalInstance.dismiss('cancel');
	// 			$scope.reloadRoute();
    //         };
	// $scope.ok=function(){
	// 	//提交校验
	//    if($scope.equipmentperson.name==undefined || $scope.equipmentperson.name==""){
	// 		$scope.equipmentperson.namecheck=true;
	// 		return;
	//    }
	//    if($scope.equipmentperson.mobile==undefined || $scope.equipmentperson.mobile==""){
	// 		$scope.equipmentperson.mobilecheck=true;
	// 		return;
	//    }	   								
	// 	var savedata={};
	// 	savedata.name=$scope.equipmentperson.name;
	// 	savedata.mobile=$scope.equipmentperson.mobile;
    //  savedata.remark=$scope.equipmentperson.remark;
	// 	$resource('/api/ac/ggc/equipmentPerManageService/create', {}, {}).save(savedata, function(res){
	// 		if(res.errcode==0){
	// 			if(confirm("是否继续添加")){
    //                  $scope.equipmentperson={};                     
	// 			}else{
	// 				  $modalInstance.dismiss('cancel');
	// 				  $scope.reloadRoute();	
	// 			}				
	// 		}else{
	// 			alert(res.errmsg);
	// 		}
    // 	});			   
	// 	};	
};